package cn.wizzer.app.web.modules.controllers.user;

import cn.wizzer.app.sys.modules.services.SysMsgService;
import cn.wizzer.app.sys.modules.services.SysMsgUserService;
import cn.wizzer.app.user.modules.models.User;
import cn.wizzer.app.user.modules.models.enums.MsgStatusEnum;
import cn.wizzer.app.user.modules.services.GroupLogUserRelationService;
import cn.wizzer.app.user.modules.services.UserChatMsgService;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.filter.ApiTokenFilter;
import cn.wizzer.app.web.commons.ulog.annotation.ULog;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.filter.ParamFilter;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.*;

@Api(value = "/api/user/msg", tags = "用户消息")
@IocBean
@At("/api/user/msg")
public class ApiUserMsgController {
    @Inject
    @Reference
    private SysMsgService msgService;
    @Inject
    @Reference
    private SysMsgUserService msgUserService;
    @Inject
    @Reference
    private UserChatMsgService userChatMsgService;
    @Inject
    @Reference
    private GroupLogUserRelationService groupLogUserRelationService;
    @Inject
    @Reference
    private UserService userService;

    /**
     * 消息列表(查询用户消息或系统消息（1条）)
     *
     * @param msgType    String 消息类型(member:用户消息,memberAll:首页提示消息/系统通知)
     * @param pageNumber int
     * @param pageSize   int
     * @return Object
     */
    @At("/list")
    @Ok("json")
    @Filters({@By(type = ApiTokenFilter.class), @By(type = ParamFilter.class)})
    public Object list(@Param("msgType") String msgType, @Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize) {
        return Result.success().addData(msgService.listPage(pageNumber, pageSize, msgType, Mvcs.getReq().getHeader("appid")));
    }

    /**
     * 删除消息
     *
     * @param id String 消息id
     * @return Object
     */
    @At("/delete")
    @Ok("json")
    @Filters({@By(type = ApiTokenFilter.class), @By(type = ParamFilter.class)})
    @ULog(tag = "用户删除消息", msg = "消息: ${id}")
    public Object delete(@ApiParam(required = true) @Param("id") String id) {
        msgUserService.vDelete(Cnd.where("userId", "=", Mvcs.getReq().getHeader("appid")).and("msgId", "=", id));
        return Result.success();
    }

    /**
     * 查询未读消息数量
     *
     * @return Object
     */
    @At("/unreadNum")
    @Ok("json")
    @Filters({@By(type = ApiTokenFilter.class)})
    public Object unreadNum() {
        String userId = Mvcs.getReq().getHeader("appid");
        User user = userService.fetch(userId);
        int msgUserUnreadNum = msgUserService.count(Cnd.where("delFlag", "=", 0).and("userId", "=", userId)
                .and("status", "=", 0));
        int chatMsgUnreadNum = 0;
        //客服不统计客服消息未读数量
        if(!user.isAttendant()){
            chatMsgUnreadNum = userChatMsgService.count(Cnd.where("delFlag", "=", 0).and("toUser", "=", userId)
                    .and("status", "=", MsgStatusEnum.UN_READ));
        }
        int groupLogUnreadNum = groupLogUserRelationService.count(Cnd.where("delFlag", "=", 0).and("userId", "=", userId)
                .and("status", "=", MsgStatusEnum.UN_READ));
        return Result.success().addData(msgUserUnreadNum + chatMsgUnreadNum + groupLogUnreadNum);
    }

    /**
     * 查询客服未读消息数量
     *
     * @return Object
     */
    @At("/chatMsg/unreadNum")
    @Ok("json")
    @Filters({@By(type = ApiTokenFilter.class)})
    public Object chatMsgUnreadNum() {
        return Result.success().addData(userChatMsgService.count(Cnd.where("delFlag", "=", 0).and("toUser", "=", Mvcs.getReq().getHeader("appid"))
                .and("status", "=", MsgStatusEnum.UN_READ)));
    }

    /**
     * 查询系统未读消息数量
     *
     * @return Object
     */
    @At("/sysMsg/unreadNum")
    @Ok("json")
    @Filters({@By(type = ApiTokenFilter.class)})
    public Object sysMsgUnreadNum() {
        return Result.success().addData(msgUserService.count(Cnd.where("delFlag", "=", 0).and("userId", "=", Mvcs.getReq().getHeader("appid"))
                .and("status", "=", 0)));
    }

    /**
     * 系统消息设置已读
     *
     * @return Object
     */
    @At("/sysMsg/read")
    @Ok("json")
    @Filters({@By(type = ApiTokenFilter.class)})
    public Object sysMsgRead(@Param("ids") String[] ids) {
        msgUserService.update(Chain.make("status", 1),
                Cnd.where("msgId", "IN", ids)
                        .and("userId", "=", Mvcs.getReq().getHeader("appid")));
        return Result.success();
    }

}
